﻿using FTBS.Models.DAO;
using FTBS.Models.Entities;
using FTBS.Models.Entities.Concert;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FTBS.Models.BLO
{
    public class ScheduleBLO
    {
        private ScheduleDAO _scheduleDao = new ScheduleDAO();
        private ShowTimeBLO _showTimeBlo = new ShowTimeBLO();
        private PromotionBLO _promotionBlo = new PromotionBLO();

        #region --- Booking Ticket ---

        public IEnumerable<Schedule> GetListSchedule()
        {
            return _scheduleDao.GetListShedule();
        }
        public List<Schedule> GetListScheduleById(int id)
        {
            return _scheduleDao.GetListScheduleById(id);
        }

        /// <summary>
        /// Get list films are showing based on FilmId in Schedule table
        /// This function is used to load all film in the first time
        /// </summary>
        /// <author>vuongvt</author>
        /// <date>2014/03/02</date>
        public List<Film> GetListOfFilmShowing()
        {
            return _scheduleDao.GetListOfFilmShowing();
        }
        public List<Film> GetListOfFilmComing()
        {
            return _scheduleDao.GetListOfFilmComing();
        }
        public List<Film> GetFilmSameCategory(int cateId, int id)
        {
            return _scheduleDao.GetListOfFilmSameCate(cateId, id);
        }
        /// <summary>
        /// Get list Theater use for Search by Theater
        /// </summary>
        /// <returns>List of Theater</returns>
        /// <author>VuongVT</author>
        public List<Theater> GetListTheaterForSearchByTheater()
        {
            return _scheduleDao.GetListTheaterForSearchByTheater();
        }

        /// <summary>
        /// Get list of Film when Choose Theater -> User for booking by Theater
        /// </summary>
        /// <param name="theaterId"></param>
        /// <returns></returns>
        public List<Film> GetListFilmWhenChooseTheater(int theaterId)
        {
            return _scheduleDao.GetListFilmWhenChooseTheater(theaterId);
        }

        /// <summary>
        /// Get list theater of film by filmId
        /// -> Use when user click to a film. Load all theater are showing for this film
        /// </summary>
        /// <param name="filmId">filmId in Schedule table</param>
        /// <returns>list of theater</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/02</date>
        public List<Theater> GetListTheaterWhenChooseFilm(int filmId)
        {
            return _scheduleDao.GetListTheaterWhenChooseFilm(filmId);
        }

        /// <summary>
        /// Get list ShowTime by ShowtimeId in Schedule table
        /// -> Use when user click to a theater. Load all showtime for this a film of this theater
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns>List of showtime</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/02</date>
        public List<ShowTimeConcertEntity> GetListShowTimeWhenChooseTheater(int theaterId, int filmId)
        {
            return _scheduleDao.GetListShowTimeWhenChooseTheater(theaterId, filmId);
        }

        //public List<ShowTimeConcertEntity> GetListShowTime()
        //{
        //    return _scheduleDao.GetListShowTime();
        //}

        /// <summary>
        /// Get room Id to select a room for booking ticket
        /// </summary>
        /// <param name="searchSchedule"></param>
        /// <returns>RoomId</returns>
        public int GetRoomIdForBooking(SearchSchedule searchSchedule)
        {
            return _scheduleDao.GetRoomIdForBooking(searchSchedule);
        }

        /// <summary>
        /// Get ScheduleId base on FilmId, RoomId, ShowTimeId, TheaterId
        /// </summary>
        /// <param name="filmId"></param>
        /// <param name="roomId"></param>
        /// <param name="showtimeId"></param>
        /// <param name="theaterId"></param>
        /// <returns>ScheduleId</returns>
        /// <author>vuongvt</author>
        public int GetScheduleId(int filmId, int roomId, int showtimeId, int theaterId)
        {
            return _scheduleDao.GetScheduleId(filmId, roomId, showtimeId, theaterId);
        }

        /// <summary>
        /// Get user's information by username
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public User GetUserInfoByUserName(string userName)
        {
            return _scheduleDao.GetUserInfoByUserName(userName);
        }

        #endregion


        #region "Manage Schedule"

        /// <summary>
        /// Check is Schedule existed in Schedule table
        /// </summary>
        /// <param name="filmId"></param>
        /// <param name="roomId"></param>
        /// <param name="showtimeId"></param>
        /// <param name="theaterId"></param>
        /// <returns>true: Existed - false: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsExistSchedule(int filmId, int roomId, int showtimeId, int theaterId, DateTime scheduleDate)
        {
            return _scheduleDao.IsExistSchedule(filmId, roomId, showtimeId, theaterId, scheduleDate);
        }

        /// <summary>
        /// Check have exist Schedule use for Edit schedule
        /// </summary>
        /// <param name="scheduleId"></param>
        /// <param name="filmId"></param>
        /// <param name="roomId"></param>
        /// <param name="showtimeId"></param>
        /// <param name="theaterId"></param>
        /// <returns>true: Existed - false: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsExistScheduleForEdit(int scheduleId, int filmId, int roomId, int showtimeId, int theaterId, DateTime scheduleDate)
        {
            return _scheduleDao.IsExistScheduleForEdit(scheduleId, filmId, roomId, showtimeId, theaterId, scheduleDate);
        }

        /// <summary>
        /// Is selected Promotion validate with Date of Schedule
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        public bool IsValidatePromotion(Schedule schedule)
        {
            return _scheduleDao.IsValidatePromotion(schedule);
        }

        /// <summary>
        /// Get all schedule information to load to list schedule
        /// </summary>
        /// <returns>List of ScheduleConcertEntity</returns>
        public List<ScheduleConcertEntity> GetAllScheduleInfo()
        {
            return _scheduleDao.GetAllScheduleInfo();
        }

        public Schedule GetScheduleById(int scheduleId)
        {
            return _scheduleDao.GetScheduleById(scheduleId);
        }

        public Promotion GetPromotionByScheduleId(Schedule schedule)
        {
            return _promotionBlo.GetPromotionByScheduleId(schedule);
        }


        /// <summary>
        /// Add new a schedule into Schedule table
        /// </summary>
        /// <param name="scheduleConcertEntity"></param>
        /// <returns>true: Success, false: Fail</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/04</date>
        public bool CreateSchedule(Schedule schedule, string userName)
        {
            var result = _scheduleDao.CreateSchedule(schedule, userName);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Edit a schedule
        /// </summary>
        /// <param name="scheduleConcertEntity"></param>
        /// <returns>true: Success, false: Fail</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/04</date>
        public bool EditSchedule(Schedule schedule, string username, bool isChangeTheaterOrRoom)
        {
            var result = _scheduleDao.EditSchedule(schedule, username, isChangeTheaterOrRoom);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Delete a schedule
        /// </summary>
        /// <param name="scheduleConcertEntity"></param>
        /// <returns>true: Success, false: Fail</returns>
        /// <author>vuongvt</author>
        /// <date>2014/03/04</date>
        public bool DeleteSchedule(Schedule schedule)
        {
            var result = _scheduleDao.DeleteSchedule(schedule);
            if (result == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Update IsCreateTicket = true when finish create Ticket
        /// </summary>
        /// <param name="schedule"></param>
        /// <returns></returns>
        /// <author>VuongVT</author>
        public int UpdateCreatedTicket(Schedule schedule)
        {
            return _scheduleDao.UpdateCreatedTicket(schedule);
        }

        /// <summary>
        /// Check the Schedule is using  and have Booked ticket or not
        /// This function is used for user Click to Edit and Delete in List Schedule
        /// </summary>
        /// <param name="showTimeId"></param>
        /// <returns>True: Is Exist ---- False: Not Exist</returns>
        /// <author>VuongVT</author>
        public bool IsScheduleUsingAndBooked(int scheduleId)
        {
            return _scheduleDao.IsScheduleUsingAndBooked(scheduleId);
        }

        /// <summary>
        /// Check Date of Schedule is smaller than Deadline for create or not
        /// This function is used when staff create Schedule
        /// </summary>
        /// <param name="showtime"></param>
        /// <returns>True: Smaller --- False: Bigger</returns>
        public bool IsScheduleSmallerThanDeadline(Schedule schedule)
        {
            return _scheduleDao.IsScheduleSmallerThanDeadline(schedule);
        }
        #endregion

        #region ---Load Films---
        /// <summary>
        /// LoadFilm in Next 7 Days
        /// </summary>
        /// <returns>ListFilm</returns>
        /// <author>nguyenht</author>
        /// <date>2014/03/06</date>
        public List<FilmConcertEntity> LoadFilmsIn7days()
        {
            return _scheduleDao.LoadFilmsCommingSoon();
        }

        /// <summary>
        /// Load all films are showing in Schedule
        /// </summary>
        /// <returns></returns>
        public List<Schedule> GetSchedulesShowing()
        {
            return _scheduleDao.LoadSchedulesShowing();
        }
        public List<Schedule> GetSchedulesPro()
        {
            return _scheduleDao.LoadSchedulesPro();
        }
        public List<Schedule> GetSchedulesComing()
        {
            return _scheduleDao.LoadScheduleComing();
        }
        public List<Promotion> GetListOListPromotion()
        {
            return _scheduleDao.GetListOListPromotion();
        }
        public List<Ticket> GetListTicket()
        {
            return _scheduleDao.LoadScheduleHot();
        }
        /// <summary>
        /// Load all films have promotion
        /// </summary>
        /// <returns></returns>
        public List<FilmConcertEntity> GetFilmsPromotion()
        {
            return _scheduleDao.LoadFilmsPromotion();
        }

        /// <summary>
        /// Get Film has max rate
        /// </summary>
        /// <returns></returns>
        public FilmConcertEntity GetFilmsBestPromtion()
        {
            return _scheduleDao.FilmBestPromotion();
        }

        /// <summary>
        /// Load films comming soon to Home Page of Guest and Customer
        /// </summary>
        /// <returns>ListFilm</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/06</date>
        public List<FilmConcertEntity> GetFilmCommingSoon()
        {
            return _scheduleDao.LoadFilmsCommingSoon();
        }

        /// <summary>
        /// Load Films are showing to Home page of guest and customer
        /// </summary>
        /// <returns>ListFilmConcertEntity</returns>
        /// <author>VuongVT</author>
        /// <date>2014/03/06</date>
        public List<Film> LoadFilmsAreShowing()
        {
            return _scheduleDao.LoadFilmsAreShowing();
        }

        #endregion

        /// <summary>
        /// select all schedules has promotionId
        /// </summary>
        /// <param name="pId"></param>
        /// <returns></returns>  
        /// <author>DUYHDV</author>
        /// <date>2014/03/29</date>
        public List<Promotion> GetCurrentPromotion()
        {
            List<Promotion> schedules = new List<Promotion>();
            schedules = _scheduleDao.GetCurrentPromotion();
            return schedules;
        }

        /// <summary>
        /// Get list showtime by date, theaterId and FilmId
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns></returns>          
        /// <author>DUYHDV</author>
        public List<ShowTimeConcertEntity> GetListShowTimeForFastBuy(int theaterId, int filmId, DateTime date)
        {
            return _scheduleDao.GetListShowTimeFastBuy(theaterId, filmId, date);
        }

        /// <summary>
        /// Set up data for bookinf by date,showtime and theaterId and FilmId
        /// </summary>
        /// <param name="showtimeId"></param>
        /// <returns></returns>          
        /// <author>DUYHDV</author>
        public List<ShowTimeConcertEntity> SetUpFastBuy(int theaterId, int filmId, DateTime date, int showtimeId)
        {
            return _scheduleDao.SetUpFastBuy(theaterId, filmId, date, showtimeId);
        }
    }
}